package domain.gbshaasrode.spreadsheet;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.WorksheetEntry;
import com.google.gdata.util.ServiceException;

import domain.DomainException;

public class GBSOpvangWorkSheetLeerlingen extends GBSOpvangWorkSheet {

	public GBSOpvangWorkSheetLeerlingen(
			GBSHaasrodeSpreadSheetService service,
			GBSOpvangSpreadSheet gbsOpvangSpreadSheet) throws DomainException {
		super(service, gbsOpvangSpreadSheet);
	}
	
	@Override
	protected void initWorkSheet() throws DomainException{
		SpreadsheetEntry spreadsheet = getGbsOpvangSpreadSheet().getGbsHaasrodeSS();
	    List<WorksheetEntry> worksheets;
		try {
			worksheets = spreadsheet.getWorksheets();
		    // Iterate through each worksheet in the spreadsheet.
		    for (WorksheetEntry worksheet : worksheets) {
		      // Get the worksheet's title, row count, and column count.
		      String title = worksheet.getTitle().getPlainText();
		      if(title.equals("Leerlingen")){
		    	  setWorksheet(worksheet);
		      }
		    }
		    if(getWorksheet() == null){
		    	throw new DomainException("worksheet not found");
		    }
		} catch (IOException | ServiceException e) {
			throw new DomainException("no worksheets available",e);
		}		
	}
	

	
	public int addRow(String naam, String voornaam, String straat, String huisnr, String postcode, String stad) throws DomainException{
	    // Fetch the list feed of the worksheet.
	    URL listFeedUrl = getWorksheet().getListFeedUrl();
	    int llnr = getWorksheet().getRowCount();
	    // Create a local representation of the new row.
	    ListEntry row = new ListEntry();
	    row.getCustomElements().setValueLocal("nr", ""+llnr);
	    row.getCustomElements().setValueLocal("naam", naam);
	    row.getCustomElements().setValueLocal("voornaam", voornaam);
	    row.getCustomElements().setValueLocal("straat", straat);
	    row.getCustomElements().setValueLocal("huisnr", huisnr);
	    row.getCustomElements().setValueLocal("pc", postcode);
	    row.getCustomElements().setValueLocal("stad", stad);

	    // Send the new row to the API for insertion.
	    try {
			row = getGbsService().getService().insert(listFeedUrl, row);
		} catch (IOException e) {
			throw new DomainException("cannot write", e);
		} catch (ServiceException e) {
			throw new DomainException("service unavailable", e);
		}
	    return llnr;
	}
	
	public ArrayList<Integer> getNrsFor(String voornaam, String achternaam) throws DomainException{
		ArrayList<Integer> llnrs = new ArrayList<Integer>();
		URL listFeedUrl = getWorksheet().getListFeedUrl();
		ListFeed listFeed;
		try {
			listFeed = getGbsService().getService().getFeed(listFeedUrl, ListFeed.class);		
			// Iterate through each row, printing its cell values.
			
			for (ListEntry row : listFeed.getEntries()) {
				String nr = row.getCustomElements().getValue("nr");			
				String ln	= row.getCustomElements().getValue("naam");		
				String fn	= row.getCustomElements().getValue("voornaam");	
				if((voornaam.equals("")||fn.equals(voornaam)) &&
						(achternaam.equals("")||ln.equals(achternaam))	){
					llnrs.add(Integer.parseInt(nr));
				}
			}	
		} catch (IOException | ServiceException e) {
			throw new DomainException("Something went wrong",e);
		}		
		return llnrs;
	}
	
	public String toString(int llnr) throws DomainException{
		String overview = "";
		URL listFeedUrl = getWorksheet().getListFeedUrl();
		try {
			listFeedUrl = new URI(getWorksheet().getListFeedUrl().toString()
					+ "?sq=nr=="+llnr).toURL();
		} catch (URISyntaxException e) {
			throw new DomainException("no entries found",e);
		} catch (MalformedURLException e) {
			throw new DomainException("wrong url",e);
		}
		ListFeed listFeed;
		try {
			listFeed = getGbsService().getService().getFeed(listFeedUrl, ListFeed.class);		
			// Iterate through each row, printing its cell values.
			
			for (ListEntry row : listFeed.getEntries()) {
				String naam	= row.getCustomElements().getValue("naam");		
				String voornaam	= row.getCustomElements().getValue("voornaam");		
				String straat = row.getCustomElements().getValue("straat");		
				String huisnr = row.getCustomElements().getValue("huisnr");		
				String pc = row.getCustomElements().getValue("pc");		
				String stad = row.getCustomElements().getValue("stad");					

				overview += "- "+naam+" "+voornaam+" "+straat+" "+huisnr+" "+pc+" "+stad+": ";
			}	
		} catch (IOException | ServiceException e) {
			throw new DomainException("Something went wrong",e);
		}		
		return overview;
	}
}
